package com.seashop.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.seashop.constant.ThirdConstant;
import com.seashop.request.GoogleTransRequestParam;
import com.seashop.service.ITranslateService;
import com.seashop.util.OkHttp3Util;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author moredln
 * @desc
 * @date 2020-08-11 11:42
 */
@Service
public class TranslateServiceImpl implements ITranslateService {

    @Override
    public List<String> batchTrans(GoogleTransRequestParam param) {
        List<String> result = null;
        try {
            result = new ArrayList<>();
            Map<String, String> params = new HashMap<>(6);
            params.put("target", param.getToLanguage());
            params.put("format", "text");
            params.put("source", param.getFromLanguage());
            // 翻译模型:nmt神经网络翻译-base机器翻译
            params.put("model", "nmt");
            params.put("key", ThirdConstant.GOOGLE_TRANSLATE_KEY);
            String response = OkHttp3Util.doPost(ThirdConstant.GOOGLE_TRANSLATE_URL, params, param.getTexts(), "q");
            if (StringUtils.isNotBlank(response)) {
                JSONObject jsonObject = JSON.parseObject(response);
                JSONObject dataJsonObject = jsonObject.getJSONObject("data");
                JSONArray translations = dataJsonObject.getJSONArray("translations");
                if (CollectionUtils.isNotEmpty(translations)) {
                    for (Object object : translations) {
                        JSONObject translatedText = (JSONObject) object;
                        result.add(translatedText.getString("translatedText"));
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        /////////////////////////////// 翻译失败的情况考虑///////////////////////////
        if (result.size() < param.getTexts().size()) {
            for (int i = param.getTexts().size() - result.size(); i < param.getTexts().size(); i++) {
                result.add(param.getTexts().get(i));
            }
        }
        /////////////////////////////// 翻译失败的情况考虑///////////////////////////
        return result;
    }

    @Override
    public String trans(String text, String fromLanguageType, String toLanguageType) {
        String result = "";
        try {
            Map<String, String> params = new HashMap<>(6);
            params.put("target", toLanguageType);
            params.put("format", "text");
            params.put("source", fromLanguageType);
            // 翻译模型:nmt神经网络翻译-base机器翻译
            params.put("model", "nmt");
            params.put("key", ThirdConstant.GOOGLE_TRANSLATE_KEY);
            String response = OkHttp3Util.doPost(ThirdConstant.GOOGLE_TRANSLATE_URL, params, Arrays.asList(text), "q");
            if (StringUtils.isNotBlank(response)) {
                JSONObject jsonObject = JSON.parseObject(response);
                JSONObject dataJsonObject = jsonObject.getJSONObject("data");
                JSONArray translations = dataJsonObject.getJSONArray("translations");
                if (CollectionUtils.isNotEmpty(translations)) {
                    for (Object object : translations) {
                        JSONObject translatedText = (JSONObject) object;
                        return translatedText.get("translatedText").toString();
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
}
